<?php
//
// +------------------------------------------------------------------------+
// | Gecko Framework                                                        |
// +------------------------------------------------------------------------+
//

/**
 * Puts a new Print Page link
 *
 * @param string $label
 * @return string
 */
function link_tag_print( $label ) {
	$url = "javascript:window.print();";

	$params = array();
	$params['href'] = $url;

	return _construct_tag( "a", $params, $label );
}

/**
 * Renders a Page URL link (onto a new page)
 *
 * @param string $label
 * @param string $url
 * @param array $sendParams
 * @param string $title
 * @return string
 */
function link_tag_new($label, $url, $sendParams = array(), $title = "") {
	$cfg = Zend_Registry::get('config');
	$params = array(
		"href" => $cfg->pageRootURL . $url,
		"title" => $title,
		"onclick" => 'window.open(this.href); return false;',
	);

	if( count( $sendParams ) != 0 ) {
		$params['href'] .= "?" . http_build_query( $sendParams );
	}

	return _construct_tag( "a", $params, $label );
}

/**
 * Creates a new uri (considering baseURL)
 *
 * @param string $uri
 * @param array $sendParams
 * @return string
 */
function uri_map( $uri, $sendParams = array() ) {
	$cfg = Zend_Registry::get('config');

	$link = $cfg->pageRootURL . $uri;

	if( count( $sendParams ) != 0 ) {
		$link .= "?" . http_build_query( $sendParams );
	}

	return $link;
}

/**
 * Creates a new URL to a specific action (on
 * same URL)
 *
 * @param string $action
 * @param array $sendParams
 * @return string
 */
function uri_to_action( $action, $sendParams = array() ) {
	$base = Zend_Registry::get( 'base' );
	$link = $base->getController()->linkToAction( $action );

	if( count( $sendParams ) != 0 ) {
		$link .= "?" . http_build_query( $sendParams );
	}

	return $link;
}

/**
 * Creates a new link to a action
 *
 * @param string $label
 * @param string $action
 * @param array $sendParams
 * @param string $title
 * @param string $target
 * @return string
 */
function link_to_action( $label, $action, $sendParams = array(), $title = "", $target = "_self" ) {
	$base = Zend_Registry::get( 'base' );
	$link = $base->getController()->linkToAction( $action );

	$params = array(
		"href" => $link,
		"target" => $target,
		"title" => $title,
	);

	if( count( $sendParams ) != 0 ) {
		$params['href'] .= "?" . http_build_query( $sendParams );
	}

	return _construct_tag( "a", $params, $label );
}

/**
 * Creates a link to a controller / action
 * taking into account base URL
 *
 * @param string $label
 * @param string $url
 * @param array $sendParams
 * @param string $title
 * @param string $target
 * @return string
 */
function link_tag( $label, $url, $sendParams = array(), $title = "", $target ="_self" ) {
	$cfg = Zend_Registry::get('config');
	$params = array(
		"href" => $cfg->pageRootURL . $url,
		"target" => $target,
		"title" => $title,
	);

	if( count( $sendParams ) != 0 ) {
		$params['href'] .= "?" . http_build_query( $sendParams );
	}

	return _construct_tag( "a", $params, $label );
}

/**
 * Creates a SWF flash tag (IE, FF, Opera Compatible)
 *
 * @param string $swf
 * @param string $width
 * @param string $height
 * @param string $align
 * @param string $transcolor
 * @param string $wmode
 * @return string
 */
function flash_tag( $swf, $width, $height, $align = 'center', $transcolor="#FFFFFF", $wmode = "transparent" ) {
	$cfg = Zend_Registry::get('config');
	$swfSrc = $cfg->pageRootURL . "/web/swf/" . $swf;
	return Gecko_HTML::makeSWF( $swfSrc, $width, $height, $align, $transcolor, $wmode );
}

/**
 * Creates a link to the same page
 *
 * @param string $label
 * @param array $params
 * @return string
 */
function link_to_self( $label, $params = array() ) {
	return Gecko_URL::genSURI( $label, '_self', $params );
}

/**
 * Creates a URL to the same page
 *
 * @param array $params
 * @return string
 */
function uri_to_self( $params = array() ) {
	return Gecko_URL::genSURI( '', '', $params );
}

/**
 * Creates a new Image Tag (loading image from web/images)
 *
 * @param string $src
 * @param string $id
 * @param array $other_params
 * @return string
 */
function image_tag( $src, $id, $other_params = array() ) {
	$cfg = Zend_Registry::get('config');
	$params = array(
		"src" => $cfg->pageRootURL . "/web/images/" . $src,
		"id" => $id,
		"border" => "0",
		"alt" => $src,
	);

	$params = array_merge( $params, $other_params );

	return _construct_tag( "img", $params );
}

/**
 * Creates a new URL to a image in image repository
 *
 * @param string $image
 * @return string
 */
function image_route( $image ) {
	$cfg = Zend_Registry::get('config');

	return $cfg->pageRootURL . "/web/images/" . $image;
}

/**
 * Inner function that creates a html tag
 *
 * @param string $tag
 * @param array $params
 * @param string $value
 * @return string
 */
function _construct_tag( $tag, $params, $value = "" ) {
	$buffer = "";

	foreach( $params as $param => $param_value ) {
		$buffer .= " " . $param . "=\"" . (string) $param_value . "\"";
	}

	if( !empty( $value ) ) {
		$out =  "<" . $tag .  $buffer . ">" . $value . "</" . $tag . ">";
	} else {
		$out =  "<" . $tag . $buffer . " />";
	}

	return $out;
}
?>